#!/usr/bin/env bash

ENVIRONMENT_TYPE=$1
ENV_DEV="dev"
ENV_DEVTEST="devtest"
ENV_DEVTEST_UPGRADE="devtest_upgrade"
ENV_PRODECT="product"

BRIDGE_NET_dev="bert-server-net"
BRIDGE_NET_devtest="bert-server-devtest-net"
BRIDGE_NET_product="bert-server-net"


BERT_SERVER_CONTAINER="bert_server_dev"

# 判断是否传参
if [ ! ${ENVIRONMENT_TYPE} ];then
    echo "请输入是哪个环境,示例：
          开发环境：sh init_docker_container_and_iamges.sh dev
          测试环境：sh init_docker_container_and_iamges.sh devtest
          测试环境 upgrade：sh init_docker_container_and_iamges devtest_upgrade
          线上环境：sh init_docker_container_and_iamges.sh product"
    exit 0
fi

######################开发环境###########################
if [ "${ENVIRONMENT_TYPE}" = "$ENV_DEV" ];then
    IMAGE_NAME="nlpservice"
    IMAGE_VERSION="v1"
    CONTAINER_NAME="nlpservice_dev"
    BUILD_CONTEXT_PAHT="nlpservice"
    PORT_MAPPING="8128:8128"            # 主机端口:容器端口
    VOLUME="/var/sharedata/nlpservice/:/nlpservice/nlpservice_data"

    BERT_SERVER_CONTAINER="bert_server_dev"


    # 构造镜像
    cd ..
    #docker build -t ${IMAGE_NAME}:${IMAGE_VERSION} -f ${BUILD_CONTEXT_PAHT}/Dockerfile .

    # 移除旧的容器
    docker stop ${CONTAINER_NAME}
    docker rm ${CONTAINER_NAME}

    # 实例化容器
    #docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice  -p ${PORT_MAPPING}  -idt --link ${BERT_SERVER_CONTAINER}:${BERT_SERVER_CONTAINER} ${IMAGE_NAME}:${IMAGE_VERSION}
    docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice -v ${VOLUME} -p ${PORT_MAPPING}  -idt ${IMAGE_NAME}:${IMAGE_VERSION} /bin/bash
    exit 0
fi
######################开发环境###########################

######################测试环境###########################
if [ "${ENVIRONMENT_TYPE}" = "$ENV_DEVTEST" ];then
    IMAGE_NAME="nlpservice"
    IMAGE_VERSION="v1"
    CONTAINER_NAME="nlpservice_devtest"
    BUILD_CONTEXT_PAHT="nlpservice"
    PORT_MAPPING="8127:8128"
    VOLUME="/var/sharedata/nlpservice/:/nlpservice/nlpservice_data"

    BERT_SERVER_CONTAINER="bert_server_devtest"

    # 构造镜像
    cd ..
    #docker build -t ${IMAGE_NAME}:${IMAGE_VERSION} -f ${BUILD_CONTEXT_PAHT}/Dockerfile .

    # 移除旧的容器
    docker stop ${CONTAINER_NAME}
    docker rm ${CONTAINER_NAME}

    # 实例化容器
#    docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice -v ${VOLUME} -p ${PORT_MAPPING} -idt --link ${BERT_SERVER_CONTAINER}:${BERT_SERVER_CONTAINER} ${IMAGE_NAME}:${IMAGE_VERSION}
    docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice -v ${VOLUME} -p ${PORT_MAPPING} -idt ${IMAGE_NAME}:${IMAGE_VERSION} /bin/bash
    exit 0
fi
######################测试环境###########################

######################线上环境###########################
if [ "${ENVIRONMENT_TYPE}" = "$ENV_PRODECT" ];then
    IMAGE_NAME="nlpservice"
    IMAGE_VERSION="v1"
    CONTAINER_NAME="nlpservice"
    BUILD_CONTEXT_PAHT="nlpservice"
    PORT_MAPPING="8128:8128"
    VOLUME="/aidata/nlpservice/:/nlpservice/nlpservice_data"
    BERT_SERVER_CONTAINER="bert_server"
    BERT_PORT_IN="5555:5555"
    BERT_POST_OUT="5556:5556"

    # 构造镜像
    cd ..
    docker build -t ${IMAGE_NAME}:${IMAGE_VERSION} -f ${BUILD_CONTEXT_PAHT}/Dockerfile .

    # 移除旧的容器
    docker stop ${CONTAINER_NAME}
    docker rm ${CONTAINER_NAME}

    # 实例化容器
    docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice -v ${VOLUME} -p ${PORT_MAPPING} -idt ${IMAGE_NAME}:${IMAGE_VERSION}
#    docker run --name ${CONTAINER_NAME} -v $PWD:/nlpservice -v ${VOLUME} -p ${PORT_MAPPING} -p ${BERT_PORT_IN} -p${BERT_POST_OUT} --network ${BRIDGE_NET} -idt ${IMAGE_NAME}:${IMAGE_VERSION} /bin/bash
    exit 0
fi
######################线上环境###########################

echo "ENVIRONMENT_TYPE Error!!!!"